$(function() {


	$('.btn_comfirm').on('click', function() {
		if(alipay == '') {
			layer.msg('请先绑定账户');
			setInterval(function() {
				window.location.href = '../pricing/pay.html';
			}, 1500)
			return false;
		}

		cash = $('.much').val();
		$('input:radio[name=radio1]').each(function() {
			if($(this).prop('checked')) {
				way = $(this).val();
			}
		});

		/*正整数正则匹配*/
		cash_money();

	})

})

/*定義全局變量*/
var money = localStorage.money;
	var wechat = localStorage.wechat;
	var alipay = localStorage.alipay;
	var alipay_name = localStorage.alipay_name;
	var way = '';
	var cash = '';
	var token = localStorage.phone;

/*监听手机返回按键*/
 return_keybord();


/*正整数正则匹配*/
function cash_money() {
	var reg = /^[1-9]\d*$/;
	var num = $(".much").val();
	if(num == '') {
		layer.msg('提現金額不能為空');
		return false;
	}
	if(reg.test(num)) {
		if(cash != '' && cash > 0) {

			if(way == 'wechat' && wechat != '') {
				layer.msg('暂不支持微信');

			} else if(way == 'alipay') {
				if(money - cash >= 0) {
					$.ajax({
						url: 'https://newapp.icandrive.cn/api/cash.php',
						type: 'post',
						dataType: 'json',
						data: {
							token: token,
							cash: cash,
							way: way,
							account: alipay
						},

						success: function(data) {

							if(data.status == 200) {

								layer.msg('提现成功');
								localStorage.removeItem('money');
								localStorage.removeItem('wechat');
								localStorage.removeItem('alipay');
								setInterval(function() {
									window.location.href = '../cash/cash.html';
								}, 1500)

							} else {
								layer.msg('系统繁忙', function() {
									history.go(0)
								}, 1000);
							}

						},
						error: function() {
							layer.msg('系统繁忙');
						}

					})
				} else {
					layer.msg('余额不足');
				}
			} else {
				layer.msg('请选择提现方式');
			}
		} else {
			layer.msg('请选择正确的提现金额');
		}
	} else {
		layer.msg('请输入整数金额')
		return false;
	}
}
